<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MediaObject extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		//$this->load->view("toolbar");
	}
	
	function download($id){
		//$id=$_POST['id'];
		$this->load->model('MediaObject');
		$name=$this->MediaObject->getMedia($id);
		$this->load->helper('download');
		//echo base_url()."uploads/".$id;
		$data = file_get_contents(base_url()."uploads/".$id . '.' . $name["0"]->type); // Read the file's contents
		force_download($name["0"]->name, $data);
		//echo ($name["0"]->name);
	}

	function upload(){
            
		if (!empty($_FILES)) {
                    
			$tempFile = $_FILES['Filedata']['tmp_name'];
			//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
			$targetPath = $_SERVER['DOCUMENT_ROOT'] . 'xpages/uploads/';
			//$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
			$this->load->model('MediaObject');
			$ext=explode('.',$_FILES['Filedata']['name']);
			$ext=$ext[sizeof($ext)-1];
			$id=$this->MediaObject->addMedia($_FILES['Filedata']['name'],"description",$ext,"url",0 ,0 ,"d",300);
			//echo "path:".$targetPath . "||";
                        //$id=1;
			$targetFile =  str_replace('//','/',$targetPath) . $id . '.' . $ext;
			//$targetFile =  str_replace('/','\\',$targetFile);
			echo $id;
			// $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
			// $fileTypes  = str_replace(';','|',$fileTypes);
			// $typesArray = split('\|',$fileTypes);
			// $fileParts  = pathinfo($_FILES['Filedata']['name']);
			
			// if (in_array($fileParts['extension'],$typesArray)) {
				// Uncomment the following line if you want to make the directory if it doesn't exist
				// mkdir(str_replace('//','/',$targetPath), 0755, true);
			if( is_uploaded_file($tempFile))
				move_uploaded_file($tempFile,$targetFile);
			else
				echo "No File uploaded";
				//echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
				
			// } else {
			// 	echo 'Invalid file type.';
			// }
		}
                else {
                    echo 'dsdlsldslldsl';
                }
		
	}

        function upload_media(){

		if (!empty($_FILES)) {

			$tempFile = $_FILES['Filedata']['tmp_name'];
			//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
			$targetPath = $_SERVER['DOCUMENT_ROOT'] . 'xpages/image_uploads/';
			//$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
			$this->load->model('MediaObject');
			$ext=explode('.',$_FILES['Filedata']['name']);
			$ext=$ext[sizeof($ext)-1];
			$id=$this->MediaObject->addMedia($_FILES['Filedata']['name'],"description",$ext,"url",0 ,0 ,"d",300);
			//echo "path:".$targetPath . "||";
                        //$id=1;
			$targetFile =  str_replace('//','/',$targetPath) . $id . '.' . $ext;
			//$targetFile =  str_replace('/','\\',$targetFile);
			echo $id . '.' . $ext;
			// $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
			// $fileTypes  = str_replace(';','|',$fileTypes);
			// $typesArray = split('\|',$fileTypes);
			// $fileParts  = pathinfo($_FILES['Filedata']['name']);

			// if (in_array($fileParts['extension'],$typesArray)) {
				// Uncomment the following line if you want to make the directory if it doesn't exist
				// mkdir(str_replace('//','/',$targetPath), 0755, true);
			if( is_uploaded_file($tempFile))
				move_uploaded_file($tempFile,$targetFile);
			else
				echo "No File uploaded";
				//echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);

			// } else {
			// 	echo 'Invalid file type.';
			// }
		}
                else {
                    echo 'dsdlsldslldsl';
                }

	}
}